package com.lineage.server.model;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.lock.DwarfForClanReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1DwarfForClanInventory extends L1Inventory {
  public static final Log _log = LogFactory.getLog(L1DwarfForClanInventory.class);
  
  private static final long serialVersionUID = 1L;
  
  private final L1Clan _clan;
  
  public L1DwarfForClanInventory(L1Clan clan) {
    this._clan = clan;
  }
  
  public synchronized void loadItems() {
    try {
      CopyOnWriteArrayList<L1ItemInstance> items = DwarfForClanReading.get()
        .loadItems(this._clan.getClanName());
      if (items != null)
        this._items = items; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public synchronized void insertItem(L1ItemInstance item) {
    try {
      DwarfForClanReading.get().insertItem(this._clan.getClanName(), item);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public synchronized void updateItem(L1ItemInstance item) {
    try {
      DwarfForClanReading.get().updateItem(item);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public synchronized void deleteItem(L1ItemInstance item) {
    try {
      this._items.remove(item);
      DwarfForClanReading.get().deleteItem(this._clan.getClanName(), item);
      World.get().removeObject((L1Object)item);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public synchronized void deleteAllItems() {
    try {
      DwarfForClanReading.get().delUserItems(this._clan.getClanName());
      this._items.clear();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void writeHistory(L1PcInstance pc, L1ItemInstance item, int count, int type) {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement(
          "INSERT INTO clan_warehouse_history SET clan_id=?, char_name=?, type=?, item_name=?, item_count=?, record_time=?");
      pstm.setInt(1, this._clan.getClanId());
      pstm.setString(2, pc.getName());
      pstm.setInt(3, type);
      pstm.setString(4, item.getWarehouseHistoryName());
      pstm.setInt(5, count);
      pstm.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void onRemoveItem(L1ItemInstance item) {
    this._items.remove(item);
  }
}
